﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class DetailLottery : WebControlBase
    {
        private long ServiceDetailID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceDetailID = Request.QueryString["servDetailId"] != null ? Int32.Parse(Request.QueryString["servDetailId"].ToString().Trim()) : -1;

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && ClientState.MemberInfo.UserType != 3)
            {
                if (ServiceDetailID == -1)
                {
                    lnkAdd.Enabled = false;
                    lnkAdd.Enabled = false;
                }
            }

            if (this.IsPostBack) return;

            if (ServiceDetailID > 0)
            {
                this.BindObject();
            }
        }

        private void BindObject()
        {
            if (ServiceDetailID != -1)
            {
                ServiceDetailsInfo objService = ServiceDetailsManager.Select(ServiceDetailID);
                if (objService != null)
                {
                    txtCmd1.Text = objService.Cmd1;
                    txtCmd2.Text = objService.Cmd2;

                    txtCode1.Text = objService.Code1;
                    txtCode2.Text = objService.Code2;

                    txtParam1.Text = objService.Param1;
                    txtParam2.Text = objService.Param2;
                    txtParam3.Text = objService.Param3;

                    txtContent1.Text = objService.Content1;
                    txtContent2.Text = objService.Content2;
                    txtContent3.Text = objService.Content3;
                    txtContent4.Text = objService.Content4;
                    txtContent5.Text = objService.Content5;

                    ddlContentType.SelectedValue = objService.ContentType.ToString();
                    ddlServiceID.SelectedValue = objService.ServiceID;
                    ddlMsgType.SelectedValue = objService.MsgType.ToString();

                    txtTotalMsg.Text = objService.NumOfMsg.ToString();

                    if (objService.Status == 1)
                        this.chkStatus.Checked = true;
                    else
                        this.chkStatus.Checked = false;
                }
            }
        }

        protected void lnkBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?module=ListServiceDetail");
        }

        protected void lnkLastUpdate_Click(object sender, EventArgs e)
        {
            // After update, system send sms to XS TIP -> XS KQ
            if (this.txtCmd1.Text.ToLower() == "xs" && this.txtCode1.Text.ToLower() == "kq")
                MessageInManager.UpdateWaitingService();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServiceDetailID != -1)
                {
                    ServiceDetailsInfo objService = ServiceDetailsManager.Select(ServiceDetailID);
                    if (objService != null)
                    {
                        objService.Cmd1 = txtCmd1.Text;
                        objService.Cmd2 = txtCmd2.Text;

                        objService.Code1 = txtCode1.Text;
                        objService.Code2 = txtCode2.Text;

                        objService.Param1 = txtParam1.Text;
                        objService.Param2 = txtParam2.Text;

                        if (txtParam1.Text.Trim() != "")
                            objService.Param3 = txtCmd1.Text.Trim() + "_" + txtCode1.Text.Trim() + "_" + txtParam1.Text.Trim();
                        else
                            objService.Param3 = txtCmd1.Text.Trim() + "_" + txtCode1.Text.Trim();

                        string[] sContents = new string[5];
                        int iCont = 0;
                        for (int i = 1; i < 6; i++)
                        {
                            TextBox txt = this.FindControl("txtContent" + i.ToString()) as TextBox;
                            if (txt != null && !txt.Text.Replace(" ", "").Equals(""))
                            {
                                sContents[iCont] = txt.Text;
                                iCont++;
                            }
                        }

                        objService.Content1 = sContents[0];
                        objService.Content2 = sContents[1];
                        objService.Content3 = sContents[2];
                        objService.Content4 = sContents[3];
                        objService.Content5 = sContents[4];


                        objService.ContentType = byte.Parse(ddlContentType.SelectedValue);
                        objService.MsgType = byte.Parse(ddlMsgType.SelectedValue);
                        objService.ProcessType = 1;
                        objService.ServiceID = ddlServiceID.SelectedValue;

                        byte j = 0;

                        if (txtContent1.Text.Replace(" ", "") != "")
                            j++;
                        if (txtContent2.Text.Replace(" ", "") != "")
                            j++;
                        if (txtContent3.Text.Replace(" ", "") != "")
                            j++;
                        if (txtContent4.Text.Replace(" ", "") != "")
                            j++;
                        if (txtContent5.Text.Replace(" ", "") != "")
                            j++;

                        objService.NumOfMsg = j;

                        if (this.chkStatus.Checked)
                            objService.Status = 1;
                        else
                            objService.Status = 0;

                        bool result = ServiceDetailsManager.Update(objService);
                        if (result)
                            lblError.Text = "Cập nhật thành công!";
                        else
                            lblError.Text = "Cập nhật lỗi!";
                    }
                }
                else
                {
                    ServiceDetailsInfo objService = new ServiceDetailsInfo();

                    objService.Cmd1 = txtCmd1.Text;
                    objService.Cmd2 = txtCmd2.Text;

                    objService.Code1 = txtCode1.Text;
                    objService.Code2 = txtCode2.Text;

                    objService.Param1 = txtParam1.Text;
                    objService.Param2 = txtParam2.Text;

                    if (txtParam1.Text.Trim() != "")
                        objService.Param3 = txtCmd1.Text.Trim() + "_" + txtCode1.Text.Trim() + "_" + txtParam1.Text.Trim();
                    else
                        objService.Param3 = txtCmd1.Text.Trim() + "_" + txtCode1.Text.Trim();

                    string[] sContents = new string[5];
                    int iCont = 0;
                    for (int i = 1; i < 6; i++)
                    {
                        TextBox txt = this.FindControl("txtContent" + i.ToString()) as TextBox;
                        if (txt != null && !txt.Text.Replace(" ", "").Equals(""))
                        {
                            sContents[iCont] = txt.Text;
                            iCont++;
                        }
                    }

                    objService.Content1 = sContents[0];
                    objService.Content2 = sContents[1];
                    objService.Content3 = sContents[2];
                    objService.Content4 = sContents[3];
                    objService.Content5 = sContents[4];

                    objService.ContentType = byte.Parse(ddlContentType.SelectedValue);
                    objService.MsgType = byte.Parse(ddlMsgType.SelectedValue);
                    objService.ProcessType = 0;
                    objService.ServiceID = ddlServiceID.SelectedValue;

                    byte j = 0;

                    if (txtContent1.Text.Replace(" ", "") != "")
                        j++;
                    if (txtContent2.Text.Replace(" ", "") != "")
                        j++;
                    if (txtContent3.Text.Replace(" ", "") != "")
                        j++;
                    if (txtContent4.Text.Replace(" ", "") != "")
                        j++;
                    if (txtContent5.Text.Replace(" ", "") != "")
                        j++;

                    objService.NumOfMsg = j;

                    if (this.chkStatus.Checked)
                        objService.Status = 1;
                    else
                        objService.Status = 0;

                    bool result = ServiceDetailsManager.Insert(objService);
                    if (result)
                        lblError.Text = "Thêm mới thành công!";
                    else
                        lblError.Text = "Thêm mới lỗi!";
                }
            }
            catch { }
        }

        protected void lnkClear_Click(object sender, EventArgs e)
        {
            txtCmd1.Text = "XS";
            txtCmd2.Text = "";
            txtCode1.Text = "";
            txtCode2.Text = "";
            txtContent1.Text = "";
            txtContent2.Text = "";
            txtContent3.Text = "";
            txtParam1.Text = "";
            txtParam2.Text = "";
            txtParam3.Text = "";
            txtTotalMsg.Text = "1";
        }
    }
}